<template>
  <div>
    <el-dialog :title="this.ruleForm.id ? '修改' : '新增'" :visible="showDialog" width="30%" :before-close="handleClose" :close-on-click-modal="false">
      <div class="content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <el-form-item label="垛号" prop="stampId">
            <el-input :disabled="this.ruleForm.id ? true : false" v-model="ruleForm.stampId" placeholder="请输入垛号"></el-input>
          </el-form-item>
          <el-form-item label="粮食类型" prop="cropType">
            <el-select style="width: 100%;" v-model="ruleForm.cropType" filterable placeholder="请选择粮食类型或输入关键词">
              <el-option v-for="item in cropinfoList" :key="item.value" :label="item.label" :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="粮食品质" prop="cropQuality">
            <el-select style="width: 100%;" v-model="ruleForm.cropQuality" filterable placeholder="请选择粮食品质或输入关键词">
              <el-option v-for="item in qualityList" :key="item.value" :label="item.label" :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="实重(公斤)" prop="">
            <el-input v-model="ruleForm.solid" placeholder="请输入实重(公斤)"></el-input>
          </el-form-item> -->
          <el-form-item  label="初始仓量(公斤)" prop="">
            <el-input v-model="ruleForm.initial" placeholder="请输入初始仓量(公斤)"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="dialog-footer">
        <el-button class="submit" @click="handleSubmit">确 认</el-button>
        <el-button class="back" @click="handleClose">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import rules from '@/utils/rules'
import { selectApi,cropinfoSelectApi } from '@/api/select'
import { getUserKey } from '@/utils/auth'
import { formatDate } from '@/utils/methods'
import {  stampAddApi,stampUpdateApi } from '@/api/cageMange'
export default {
  props: ['showDialog','setTableData'],
  data() {
    return {
      ruleForm: {
        farmCode:this.$farmCode,
        teamCode:this.$teamCode,
        stampId: '',
        cropQuality: '',
        cropType: '',
        solid:0,
        initial:0,
        batch:"1"
      },
      cropinfoList:[],
      qualityList: [],
      rules: rules
    };
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        farmCode:this.$farmCode,
        teamCode:this.$teamCode,
        stampId: '',
        cropQuality: '',
        cropType: '',
        solid:0,
        initial:0,
        batch:"1"
      }
      this.$refs.ruleForm.resetFields();
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            stampUpdateApi(this.ruleForm).then((res)=>{
              this.$emit('update:showDialog', false)
              this.ruleForm = {
                farmCode:this.$farmCode,
                teamCode:this.$teamCode,
                stampId: '',
                cropQuality: '',
                cropType: '',
                solid:0,
                initial:0,
                batch:"1"
              }
              this.$refs.ruleForm.resetFields();
              this.$message({
                type:'success',
                message: '修改成功!'
              })
              this.$emit('getTable')
            })

          } else {
            if(this.ruleForm.initial>0){
              this.ruleForm.solid=this.ruleForm.initial
            }
            stampAddApi(this.ruleForm).then((res)=>{
              // this.setTableData(this.ruleForm,'add')
              this.$emit('update:showDialog', false)
                this.$emit('getTable')
                this.ruleForm = {
                  farmCode:this.$farmCode,
                  teamCode:this.$teamCode,
                  stampId: '',
                  cropQuality: '',
                  cropType: '',
                  solid:0,
                  initial:0,
                  batch:"1"
                }
                this.$message({
                  type:'success',
                  message: '添加成功!'
                })
                this.$refs.ruleForm.resetFields();
            })


          }


        } else {
          return false;
        }
      });
    },
    // 获取填报人时间
    handleGetInfo() {
      this.ruleForm.createUser = getUserKey() || 'nkjtcyf'
      this.ruleForm.createTime = formatDate(new Date())
    },
    getSelectList(){
      // quality
      selectApi({type:'quality'}).then((res)=>{
        this.qualityList=res.data.dictItemList
      })
      cropinfoSelectApi().then((res)=>{
        this.cropinfoList=res.data
      })
    }
  },
  mounted(){
    this.getSelectList()
    // this.$refs.ruleForm.resetFields();
  }
}
</script>

<style lang='scss' scoped></style>
